package edu.nf.ch02.common;

import io.minio.BucketExistsArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import java.io.InputStream;

/**
 * @ClassName MinioOptions
 * @Description TODO
 * @Author Kx
 * @Date 2025/9/4 20:30
 * @Version 1.0
 */
public class MinioOptions {
    private MinioClient client; // minio客户端
    private String bucket; // 存储桶名称

    public MinioOptions(MinioClient client, String bucket) {
        this.client = client;
        this.bucket = bucket;
        initBucket();
    }

    /**
     * @author: kx
     * @date: 2025/9/4 20:39
     * @param path : 文件路径
     * @param contentType : 文件类型
     * @param size : 文件大小
     * @param input : 文件输入流
     * @description: 上传文件
     */
    public void upload(String path, String contentType, Long size, InputStream input){
        try {
            client.putObject(PutObjectArgs.builder().bucket(bucket).object(path).contentType(contentType).stream(input, size, -1).build());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * @author: kx
     * @date: 2025/9/4 22:40
     * @description: 初始化桶
    */
    public void initBucket(){
        try {
            // 判断桶是否存在
            if (!client.bucketExists(BucketExistsArgs.builder().bucket(bucket).build())) {
                client.makeBucket(MakeBucketArgs.builder().bucket(bucket).build());
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
